package com.yanqu.road.dao.impl.sacrifice;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.config.sacrifice.SacrificeDetail;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;

public class SacrificeDetailDaoImpl extends TempDataObjectDao<SacrificeDetail> {

    public LinkedList<SacrificeDetail> getSacrificeDetailList(long dateMark, int maxCount) {
        ResultSet rs = null;
        CallableStatement call = null;
        LinkedList<SacrificeDetail> dataList = new LinkedList<>();
        String sql = "select * from t_u_sacrifice_detail where dateMark <=? order by dateMark desc limit ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, dateMark);
            call.setInt(2, maxCount);
            rs = call.executeQuery();
            while (rs.next()) {
                SacrificeDetail data = parse(rs);
                dataList.addLast(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    @Override
    protected boolean addData(SacrificeDetail data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_sacrifice_detail(dateMark,bossLevel,totalBlood,currentBlood,activeUserCount,activeAbility,inTimeUserCount,inTimeAbility,"
                + "joinUserCount,joinAbility,killBossUserId,killBossNickName,isSendReward,killBossDecoration,killTime) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getDateMark());
            call.setInt(i++, data.getBossLevel());
            call.setString(i++, data.getTotalBlood().toString());
            call.setString(i++, data.getCurrentBlood().toString());
            call.setLong(i++, data.getActiveUserCount());
            call.setString(i++, data.getActiveAbility().toString());
            call.setLong(i++, data.getInTimeUserCount());
            call.setString(i++, data.getInTimeAbility().toString());
            call.setLong(i++, data.getJoinUserCount());
            call.setString(i++, data.getJoinAbility().toString());
            call.setLong(i++, data.getKillBossUserId());
            call.setString(i++, data.getKillBossNickName());
            call.setBoolean(i++, data.isSendReward());
            call.setString(i++, data.getKillBossDecoration());
            call.setLong(i++, data.getKillTime());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(SacrificeDetail data) {
        PreparedStatement call = null;
        String sql = "update t_u_sacrifice_detail set bossLevel=?,totalBlood=?,currentBlood=?,activeUserCount=?,activeAbility=?,inTimeUserCount=?,inTimeAbility=?,"
                + "joinUserCount=?,joinAbility=?,killBossUserId=?,killBossNickName=?,isSendReward=?,killBossDecoration=?,killTime=? where dateMark=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getBossLevel());
            call.setString(i++, data.getTotalBlood().toString());
            call.setString(i++, data.getCurrentBlood().toString());
            call.setLong(i++, data.getActiveUserCount());
            call.setString(i++, data.getActiveAbility().toString());
            call.setLong(i++, data.getInTimeUserCount());
            call.setString(i++, data.getInTimeAbility().toString());
            call.setLong(i++, data.getJoinUserCount());
            call.setString(i++, data.getJoinAbility().toString());
            call.setLong(i++, data.getKillBossUserId());
            call.setString(i++, data.getKillBossNickName());
            call.setBoolean(i++, data.isSendReward());
            call.setString(i++, data.getKillBossDecoration());
            call.setLong(i++, data.getKillTime());
            call.setLong(i++, data.getDateMark());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected SacrificeDetail parseData(ResultSet rs) throws SQLException {
        SacrificeDetail data = new SacrificeDetail();
        data.setDateMark(rs.getLong("dateMark"));
        data.setBossLevel(rs.getInt("bossLevel"));
        data.setKillBossUserId(rs.getLong("killBossUserId"));
        data.setKillBossNickName(rs.getString("killBossNickName"));
        String value = rs.getString("totalBlood");
        if(!StringUtils.isNullOrEmpty(value)){
            data.setTotalBlood(new BigInteger(value));
        }
        value = rs.getString("currentBlood");
        if(!StringUtils.isNullOrEmpty(value)){
            data.setCurrentBlood(new BigInteger(value));
        }
        data.setActiveUserCount(rs.getLong("activeUserCount"));
        value = rs.getString("activeAbility");
        if(!StringUtils.isNullOrEmpty(value)){
            data.setActiveAbility(new BigInteger(value));
        }
        data.setInTimeUserCount(rs.getLong("inTimeUserCount"));
        value = rs.getString("inTimeAbility");
        if(!StringUtils.isNullOrEmpty(value)){
            data.setInTimeAbility(new BigInteger(value));
        }
        data.setJoinUserCount(rs.getLong("joinUserCount"));
        value = rs.getString("joinAbility");
        if(!StringUtils.isNullOrEmpty(value)){
            data.setJoinAbility(new BigInteger(value));
        }
        data.setSendReward(rs.getBoolean("isSendReward"));
        value = rs.getString("killBossDecoration");
        if(!StringUtils.isNullOrEmpty(value)){
            data.setKillBossDecoration(value);
        }
        data.setKillTime(rs.getLong("killTime"));
        return data;
    }
}
